package ru.amse.tsyganov.jumleditor.view;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;

import ru.amse.tsyganov.jumleditor.model.ActionState;
import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.properties.EnumProperty;
import ru.amse.tsyganov.jumleditor.properties.PropertiesList;

public class ActionStateView<T extends ActionState> extends StateView<T> {
	
	private static final int HORIZONTAL_MARGIN = 10;
	private static final int VERTICAL_MARGIN = 5;
	private static final int RADIUS = 20;
	
	public ActionStateView(T model, 
			ActivityGraphView<? extends ActivityGraph> activityGraphView) {
		
		super(model, activityGraphView);
		getModel().setName("Action state");
		initProperties();
	}
	
	@Override
	public void paintFigure(GC gc) {
		assert gc != null;
		
		gc.setBackground(Colors.getGreenColor());
		gc.fillRoundRectangle(getX(), getY(), getWidth(), getHeight(), 
				RADIUS, RADIUS);
		
		gc.setForeground(Colors.getBlackColor());
		gc.drawRoundRectangle(getX(), getY(), getWidth(), getHeight(), 
				RADIUS, RADIUS);		

		String text = getModel().getName();
		Point extent = gc.stringExtent(text);
		
		gc.drawText(text, getX() + (getWidth() - extent.x) / 2, 
				getY() + (getHeight() - extent.y) / 2);
	}
	
	@Override
	public void prepaintEvent(GC gc) {
		String text = getModel().getName();
		
		Point extent = gc.stringExtent(text);
		
		if (getWidth() < extent.x + 2 * HORIZONTAL_MARGIN) {
			setWidth(extent.x + 2 * HORIZONTAL_MARGIN);
		}
		if (getHeight() < extent.y + 2 * VERTICAL_MARGIN) {
			setHeight(extent.y + 2 * VERTICAL_MARGIN);
		}
	}

	private void initProperties() {
		super.getProperties().addProperty(new EnumProperty(new String[]{
				ActionState.CallActionMode.SYNCHRONOUS.toString(),
				ActionState.CallActionMode.ASYNCHRONOUS.toString()}) {

			@Override
			public String getPropertyName() {
				return "Call action mode";
			}

			@Override
			public Integer getPropertyValue() {
				return getModel().getCallActionMode().ordinal();
			}

			@Override
			public void setPropertyValue(Integer newValue) {
				getModel().setCallActionMode(
					ActionState.CallActionMode.values()[newValue]);
			}
			
		});
	}
	
	@Override
	public PropertiesList getProperties() {
		return super.getProperties();
	}
}
